#!/usr/bin/python
#this script will connect to the tracker and request the information for the hardcoded hash which is for the linux.torrent file
#it will save the response to a file named "probe" which is used in the peerProbe.py script
#this scipt will then wait for someone to connect and see what they send to us until they disconnect
import string
import os
import socket

myip = socket.gethostbyname(socket.gethostname());
print "ip is "+myip;

HOST = "tracker.publicbt.com"
PORT = 80	

getpath = '/announce'
info_hash = "%C8%63%65%9D%10%5E%9C%93%E9%4C%59%23%82%F5%2B%60%CE%DB%05%44";
peer_id = '%A0%70%00%00%00%00%00%00%07%77%A0%70%00%00%00%00%00%00%07%77'; 
port = "7778";
uploaded = "0";
downloaded = "0";
left = '1470814208'; #filesize - downloaded
compact = '1';
#no_peer_id 
event = "started";
ip = myip;
numwant = '10';
#key
#tackerid

request = "GET " + getpath + \
	"?info_hash=" + info_hash + \
	"&peer_id=" + peer_id + \
	"&port=" + port + \
	"&uploaded=" + uploaded + \
	"&downloaded=" + downloaded + \
	"&left=" + left + \
	"&compact=" + compact + \
	"&event=" + event + \
	"&ip=" + ip + \
	"&numwant=" + numwant + " HTTP/1.1\r\n\r\n"

s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.connect((HOST,PORT))

print "sending request \n<" + request + ">"
s.send(request)
data = s.recv(4096)
print "recv: \n<"+data+">"
probe = open("probe","wb")
probe.write(data)
probe.close()

asock = socket.socket(socket.AF_INET,socket.SOCK_STREAM);
asock.bind(("",int(port)))
asock.listen(1)
prequest = open("peerRequest","wb")
print "waiting for someone to connect: \n\n"
alldata = []
conn, addr = asock.accept()
print "connection from ", addr, "\n\n"
while 1:
	data = conn.recv(4096)
	pdata = data 
	alldata.append(data)
	print "\nrecv:\n<"
	for c in pdata:
		if not c in string.letters:
			print ord(c),
		else:
			print c,
	print ">"
	if not data: 
		break
conn.close()
for i in alldata:
	prequest.write(i)
